﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class XmlNodeCollection : IXmlNodeCollection
    {
        int index = -1;

        List<IXmlNode> nodes = null;

        public XmlNodeCollection(List<IXmlNode> nodes)
        {
            this.nodes = nodes;
        }

        public IXmlNode Add(string elementName)
        {
            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementName.");

            elementName = elementName.Trim();

            if (Contains(elementName))
                throw new Exception("node with ElementName already existed. ElementName = " + elementName);

            IXmlNode rnode = new XmlNode(elementName);

            nodes.Add(rnode);

            return rnode;
        }

        public IXmlNode Add(string elementName, string text)
        {
            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementName.");

            elementName = elementName.Trim();

            if (Contains(elementName))
                throw new Exception("node with ElementName already existed. ElementName = " + elementName);

            IXmlNode node = new XmlNode(elementName);

            node.Text = text;

            nodes.Add(node);

            return node;
        }

        public void Add(IXmlNode node)
        {
            if (node == null)
                throw new Exception("missing node.");

            if (string.IsNullOrEmpty(node.ElementName))
                throw new Exception("missing ElementName for node.");

            if (Contains(node.ElementName))
                throw new Exception("node with ElementName already existed. ElementName = " + node.ElementName);

            nodes.Add(node);
        }

        public IXmlNode this[int index]
        {
            get
            {
                if (index < 0 || index > nodes.Count - 1)
                    throw new Exception("index exceed nodes.count = " + nodes.Count);

                return nodes[index];
            }
        }

        public IXmlNode this[string elementName]
        {
            get
            {
                if (string.IsNullOrEmpty(elementName))
                    throw new Exception("missing elementname.");

                elementName = elementName.Trim();

                foreach (XmlNode node in nodes)
                {
                    if (string.Equals(node.ElementName, elementName, StringComparison.OrdinalIgnoreCase))
                        return node;
                }

                return null;
                //return new NullableXmlNode();
                //throw new Exception("node with elementname do not existed. elementname = " + elementName);
            }
        }

        public IXmlNode Remove(string elementName)
        {
            IXmlNode rnode = this[elementName];

            if (rnode == null)
                return null;

            nodes.Remove(rnode);

            return rnode;
        }

        public bool Contains(string elementName)
        {
            if (string.IsNullOrEmpty(elementName))
                return false;

            elementName = elementName.Trim();

            foreach (XmlNode node in nodes)
            {
                if (string.Equals(node.ElementName, elementName, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public int Count
        {
            get
            {
                return nodes.Count;
            }
        }

        public void Clear()
        {
            nodes.Clear();
        }

        #region IEnumerable<IXmlNode> 成员

        public IEnumerator<IXmlNode> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IXmlNode> 成员

        public IXmlNode Current
        {
            get
            {
                return this[index];
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this[index];
            }
        }

        public bool MoveNext()
        {
            if (index >= nodes.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        public override string ToString()
        {
            return "node.count = " + nodes.Count;
        }
    }
}
